//
//  VMCApi.m
//  vmcshop
//
//  Created by bigknife on 14/11/1.
//  Copyright (c) 2014年 idongler. All rights reserved.
//

#import "VMCApi.h"
#import "VMCConstants.h"
#import "UIView+Toast.h"


@implementation VMCApi
- (id)init {
	//重写init方法 设置不同的请求
    self = [super init];
    if (self) {
        //ENT API Base
        //self.apiBaseUrl = @"http://new.yaoking.cn/openapi/";
		self.apiBaseUrl = BASEURL;
        self.timeoutInSeconds = 20;//20秒超时
		
        //ENT API Sign Token
		//在这里拿到token进行拼接
        self.signToken = API_SIGN_TOKEN;
    }
    
    return self;
}
- (void)postApi:(NSString *)api
     andRequest:(ApiRequest *)apiRequest
       callback:(API_CALLBACK)callback{
    [self requestApi:api WithMethod:@"POST" andRequest:apiRequest callback:callback];
}

- (void)handleError:(NSInteger)statusCode response:(ApiResponse *)apiResponse {
    
    if (statusCode == 0)
	{
        //ApiError_ClientError
        
//        [[UIApplication sharedApplication].keyWindow makeToast:NSLocalizedString(@"信号受到干扰，请移步再试~", nil)
//                                                      duration:kVMCToastDuration
//                                                      position:@"center"];
    }
	else if (statusCode == 200)
	{
        //服务端异常
        if (apiResponse.msg && apiResponse.msg != (id) [NSNull null])
		{
//            [[UIApplication sharedApplication].keyWindow makeToast:apiResponse.msg
//                                                          duration:kVMCToastDuration
//                                                          position:@"bottom"];
        }
		else if (!apiResponse.success)
		{
            NSString *msg = apiResponse.msg;
            if (!msg || msg == (id) [NSNull null]) {
                //未知异常
				msg = NSLocalizedString(@"网络异常，请移步再试~", nil);
            }
            [[UIApplication sharedApplication].keyWindow makeToast:msg
                                                          duration:kVMCToastDuration
                                                          position:@"center"];
        }
    }
	else if (statusCode == 403)
	{
        [[UIApplication sharedApplication].keyWindow makeToast:NSLocalizedString(@"ApiError_NotLogin", nil)
                                                      duration:kVMCToastDuration
                                                      position:@"center"];
    }
	else if (statusCode >= 500)
	{
        [[UIApplication sharedApplication].keyWindow makeToast:NSLocalizedString(@"ApiError_ServerError", nil)
                                                      duration:kVMCToastDuration
													position:@"center"];
    }
	
}
@end
